using System.Collections.Generic;
using UnityEngine;
using View;
using ZWFrameWork;

public class PopActView
{
    public View.View _view;
    public bool _open;
    public int _curCount;
    public bool _getValueEnd;
    
    public int _id;
    public string _viewname;
    public int _order;
    /// <summary>
    /// 概率
    /// </summary>
    public int _probability;
    public int _popCount;
}
public class PopActivityManager : SingletonInstance<PopActivityManager>
{
    private struct popacttable
    {
        public int _id;
        public string _viewname;
        public int _order;
        /// <summary>
        /// 概率
        /// </summary>
        public int _probability;
        public int _popCount;
    }
    /// <summary>
    /// 初始化数据
    /// </summary>
    public override void InitData()
    {
        if (mPopTable.Count <= 0)
        {
            TextAsset poptable = ResourceManager.Instance.LoadAsset<TextAsset>("Hall/Table", "popactivity",".txt");
            string str = poptable.text.Replace("\r\n", "");
            string[]lines = str.Split("|");
            for (int i = 0; i < lines.Length; i++)
            {
                string[] valuearr = lines[i].Split(",");
                PopActView info = new PopActView();
                info._id = int.Parse(valuearr[0]);
                info._viewname = valuearr[1];
                info._order = int.Parse(valuearr[2]);
                info._probability = int.Parse(valuearr[3]);
                info._popCount = int.Parse(valuearr[4]);
                mPopTable.Add(info);
            }
            mPopTable.Sort((a, b) =>
            {
                if (a._order < b._order)
                {
                    return 1;
                }
                else if (a._order > b._order)
                {
                    return -1;
                }
                return 0;
            });
            Debug.LogError("加载 popactivity 表格："+poptable.text);
        }

    }

    /// <summary>
    /// 清理数据
    /// </summary>
    public override void ClearData()
    {
        mPopActList.Clear();
    }

    private List<PopActView> mPopTable = new List<PopActView>();
    
    private List<PopActView> mPopActList = new List<PopActView>();
    private int mFirstPopTime = 0;
    private int mActCount = 0;
    private bool misFirstOpen = true;
    
    public void OnResetPopActList()
    {
        mPopActList.Clear();
        for (int i = 0; i < mPopTable.Count; i++)
        {
            mPopTable[i]._open = false;
            mPopTable[i]._getValueEnd = false;
        }

        mFirstPopTime = 0;
        misFirstOpen = true;
        mActCount = 0;
    }

    public void OnCheckActChangeStatus(string viewname,bool isopen)
    {
#if UNITY_EDITOR
        OnBlockOutPopActView();
        return;
#endif
        mActCount++;
        if (mActCount > mPopTable.Count)
        {
            return;
        }
        if (isopen && mPopTable.Count > 0 && mFirstPopTime <= 3)
        {
            for (int i = 0; i < mPopTable.Count; i++)
            {
                if (mPopTable[i]._viewname == viewname)
                {
                    int range = Random.Range(1, 100);
                    Debug.LogError("活动："+viewname+
                                   "概率："+range+"<="+ mPopTable[i]._probability+
                                   ",未添加过:"+mPopTable[i]._getValueEnd+
                                   ",弹出次数:"+mPopTable[i]._curCount+"<"+mPopTable[i]._popCount);
                    if (mPopTable[i]._getValueEnd == false && //未添加过
                        range <= mPopTable[i]._probability && //概率
                        mPopTable[i]._curCount < mPopTable[i]._popCount)//弹出次数
                    {
                        mPopTable[i]._curCount++;
                        mPopTable[i]._getValueEnd = true;
                        
                        mPopActList.Add(mPopTable[i]);
                        mFirstPopTime++;
                        break;
                    }
                }
            }
        }

        Debug.LogError(mActCount+"有三个数据==============："+mPopActList.Count);
        if (mActCount == mPopTable.Count)
        {
            mPopActList.Sort((a, b) =>
            {
                if (a._order < b._order)
                {
                    return 1;
                }
                else if (a._order > b._order)
                {
                    return -1;
                }
                return 0;
            });
            OnShowPopView();
        }
    }

    /// <summary>
    /// 打开一个界面
    /// </summary>
    public void OnShowPopView()
    {
        if (GlobalUserItem.KindID <= -1)
        {
            OnBlockOutPopActView();
            return;
        }
        if (mPopActList.Count > 0)
        {
            if (mPopActList[0]._id == 1)
            {
                UIManager.Instance.Show(mPopActList[0]._viewname,1);
            }
            else
            {
                UIManager.Instance.Show(mPopActList[0]._viewname);
            }
            mPopActList.RemoveAt(0);
            // Debug.LogError("打开一个之后还有几个==============："+mPopActList.Count);
        }
    }

    /// <summary>
    /// 阻断弹窗
    /// </summary>
    public void OnBlockOutPopActView()
    {
        // Debug.LogError("阻断弹窗==============："+mPopActList.Count);
        mPopActList.Clear();
    }

    /// <summary>
    /// 判断打开的是不是队列里的，如果不是就清理整个列表
    /// </summary>
    /// <param name="name"></param>
    public void OnCheckViewIsPopStack(string name)
    {
        // var list = mPopActList.ToArray();
        for (int i = 0; i < mPopActList.Count; i++)
        {
            if (name.Contains(mPopActList[i]._viewname))
            {
                return;
            }
        }

        OnBlockOutPopActView();
    }
}
